proc useradd {nick handle chan otbn1 host otbn tcl} {
# Changelog
# ---------
#   Bugs - Version 3.0 (Bot-Solutions.TCL / The X-Bot Tcl 3.0)
#	- Basically the bot never added correct hostmasks to the userfile so
#	  checking if a valid host was being used would always yield a false result
#	  eventually causing users to be "deleted"
#	History: Inherited from OTBN.TCL 2.0 - Bug remains in OTBN.TCL 2.2
#		The programmers of OTBN.TCL 2.2 refuse to fix this bug
# ========================================================================================
# This is a manual recoding and documentation by Overtech Technologies
#  In accordance with international regulations, this code is released under GPL version 2
# ========================================================================================
# Personal Comments
# -----------------
#  Poorly or undocumented code leads to security holes and backdoors.  
#  Duplicated code leads to security holes and backdoors
#  These are traits of OTBN.TCL 1.x and 2.x.
#
#  Therefore: 
#	Using Bot-Solutions' release of OTBN.tcl leads to security holes and backdoors in your bot
#
#  A friendly reminder from Overtech Technologies ( http://www.OvertechTechnologies.com )
# ========================================================================================
# Variable Documentation
# ----------------------
# $otbn1 = the "rest" variable from other scripts, 
#	user issues command:
#		x-bot useradd |nsid3r ccadmin
#		$botnick useradd $otbn1 $otbn
#	
#		$nick	 = nickname of the user issuing command
#		$botnick = (derived from global variable) nickname of the bot
#		$otbn1 = nickname of the user to be added
#		$otbn = rest variable (includes access level and if for global access)
#	
#	$host is derived automatically from $nick by eggdrop
#		stored as: user@host
#	$handle is the eggdrop version of $host, used for eggdrop functions
#
#	$tcl - The purpose of $tcl is unknown.  This variable is potentially used for
#	a backdoor created by Bot-Solutions.  This backdoor has been admitted to by Bot-Solutions
#	staff and coders.  Note, $tcl is not used anywhere in this procedure.
####################################################################################

# Grab global variables 
# ---------------------
  global home botnick bcast
# in OTBN.tcl 2.2 - $home and $bcast are not pulled from the global variable pool


# Verify user isn't trying to add the bot to itself - this can cause major problems
# ----------------------------------------------------------------------------------
  if { [string tolower $botnick] == [string tolower $otbn1] } { 
    putquick "NOTICE $nick :\0034ERROR - A bot cannot be adduser'd to itself."
    return 0

# Verify sufficient arguments were given by the user
# --------------------------------------------------
  } elseif {$otbn1 == ""} {
    putquick "NOTICE $nick :\0034ERROR - Invalid Syntax - Should Be - (/msg) $botnick useradd <nick> <#channel> <level>"
    return 0
  } elseif {$otbn == ""} {
    putquick "NOTICE $nick :\0034ERROR - Unknown Level - use one of these levels: avoice,aop,op,ccadmin,cadmin,bop,bcadmin,root"
    return 0
  }

# Bot-Solutions converted the user's nickname to lowercase, we can play along with that
#   besides, it makes our if statements easier :)
# ----------------------------------------------------------------------------------
  set otbn [string tolower $otbn]

# Get the hostname of the user based on the channel that they're being added to (we know they're already in there)
# Will return "" if user not on that channel.  If that is the case, who cares - the user wont be added anyway.
# ----------------------------------------------------------------------------------
  set xnewhost [getchanhost $otbn1 $chan]

# Need a nick!user@host for hostmask for adduser command
# ------------------------------------------------------
  set xhostmask "$otbn1!$xnewhost"

# Set default value for new user ( 0 = false / 1 = true )
# ------------------------------
  set newuser 1

####################################################################################
# Preconditions:
# - All variables have been declared
#################################################################################### 
# Verify if user is being added to channel X that the user is currently on channel X
# ----------------------------------------------------------------------------------
  if {![onchan $otbn1 $chan]} {
    putquick "NOTICE $nick :\0034ERROR - $otbn1 not found on $chan"
    return 0
  }

# ----------------------------------------------------------------------------------
# OOPs on part of the Bot-Solutions staff.  ~ Became a control character
# and they never fixed that.  Note, eggdrop now has far better hostname handling
# so most of the below code has/will have to be re-written
# ----------------------------------------------------------------------------------


# Does this person even have enough access to add any users?
# ----------------------------------------------------------------------------------
  # Taken from avoice since that's the lowest user level if you don't count public
  if {![matchattr $handle o|m $chan]} {
    putquick "NOTICE $nick :\0034Unable To Execute Command - Permission Denied."
    return 0
  }


# Is this user currently added to the channel access list?
# ----------------------------------------------------------------------------------
  if {[matchattr $otbn1 |+f $chan]} {
    # Yes, it would be nice to automatically use the usermod command but this could
    #   be more of a security risk than useful.  Besides, I personally like these messages
    putquick "NOTICE $nick :\0034ERROR - Unable to add user - the user $otbn1 currently exists in $chan.  Try using the usermod command instead."
    return 0
  }

# Guard agaist common non-existant access levels
# ----------------------------------------------------------------------------------

# Voice
  if {[string match {voice} $otbn] == 1 } {  
    putquick "NOTICE $nick :\0034Unable To Execute Command - Voice level does not exist. Try using AVoice instead."
    return 0
  }

# Numeric User Levels
  if {[string match {%0} $otbn] == 1 || [string match {%1} $otbn] == 1 || [string match {%2} $otbn] == 1 || [string match {%3} $otbn] == 1 || [string match {%4} $otbn] == 1 || [string match {%5} $otbn] == 1 || [string match {%6} $otbn] == 1 || [string match {%7} $otbn] == 1 || [string match {%8} $otbn] == 1 || [string match {%9} $otbn] == 1 } {  
    putquick "NOTICE $nick :\0034Unable To Execute Command - X-Bot does not use numeric user levels. Type $botnick help useradd for acceptable user levels."
    return 0
  }

# Badmin
  if {[string match {badmin} $otbn] == 1 } {
    putquick "NOTICE $nick :\0034Unable To Execute Command - Use Root instead of BAdmin for a user level."
    return 0    
  }

# Ensure only the levels we want remain, give a generalized error for all other screw-ups by the user
  if {[string match {avoice} $otbn] == 1 || [string match {op} $otbn] == 1 || [string match {aop} $otbn] == 1 || [string match {ccadmin} $otbn] == 1 || [string match {cadmin} $otbn] == 1 || [string match {bop} $otbn] == 1 || [string match {bcadmin} $otbn] == 1 || [string match {root} $otbn] == 1 } {  
	# cool, valid user level, keep going
  } else {
    putquick "NOTICE $nick :\0034Unable To Execute Command - \00312$otbn\0034 is not a valid user level. Type $botnick help useradd for acceptable user levels."
    return 0         
  }


####################################################################################
# Preconditions:
# - All erroneous input has been filtered
####################################################################################
# Create new user record if needed
# --------------------------------
  set newuser [adduser $otbn1 $xhostmask]
  # adduser returns 1 if successful (new user) / 0 if user already exists

  set xuserhandle [nick2hand $otbn1 $chan]

# Set Chat Attributes appropriate for the level
# Updated since Bot-Solutions.tcl to allow for the new global|channel format for this command
  if {[string match {avoice} $otbn] == 1 } {
    chattr $xuserhandle +hp|+fv $chan
  } elseif { [string match {op} $otbn] == 1 } {
    chattr $xuserhandle +hp|+fo $chan
  } elseif { [string match {aop} $otbn] == 1 } {
    chattr $xuserhandle +hp|+af $chan
  } elseif { [string match {ccadmin} $otbn] == 1 } {
    chattr $xuserhandle +hp|+fmo $chan
  } elseif { [string match {cadmin} $otbn] == 1 } {
    chattr $xuserhandle +hp|+fmno $chan
  } elseif { [string match {bop} $otbn] == 1 } {
    chattr $xuserhandle +ofpxh 
  } elseif { [string match {bcadmin} $otbn] == 1 } {
    chattr $xuserhandle +fhjmoptx
  } elseif { [string match {root} $otbn] == 1 } {   
    chattr $xuserhandle +fhjmnoptx
  } 

####################################################################################
# Preconditions: 
# - All operations were successful as of this point forward
# - The user has been added to the bot
####################################################################################

# Save everything done above
# --------------------------
  save

# Notify user they have been added to the bot
# ----------------------------------------------------------------------------------
  putquick "NOTICE $otbn1 :\0033You have been added into my user list by: \0034\002$nick\002"


# If new user, tell them to wait before setting pass - can cause probs if they don't
# ----------------------------------------------------------------------------------
  if {$newuser == 1} {
    putquick "NOTICE $otbn1 :\00312Do not set pass until you are instructed to do so..."
  }

# If broadcasting is on, notify home channel immediately of new user
# ----------------------------------------------------------------------------------
  if {$bcast == "1"} {
    putquick "PRIVMSG $home :\0034\002$nick\002\0033 (\0034$host\0033) requested me to useradd \0034\002$otbn1\002\0033 into my user partition in channel record: '\0034$chan\0033'. Level: \0034$otbn"
  }

# Log this useradd
# ----------------------------------------------------------------------------------
  putlog "!$nick ($host)! useradd $chan $otbn1 $otbn"

# Tell the user their info according to the bot
# ----------------------------------------------------------------------------------
  putquick "NOTICE $otbn1 :\0033Here is your access information for $chan...."
  do_access $otbn1 $chan $otbn1 $xnewhost


# Tell the person who added this info the new user's info according to the bot
# ----------------------------------------------------------------------------------
  putquick "NOTICE $nick :\0033Sucessfully Added $otbn1 - Here is their access information for $chan:"
  do_access $nick $chan $otbn1 $host

# Tell user they have a pass set unless they're a new user, in which case tell them
# to set pass
# ----------------------------------------------------------------------------------
  if {$newuser == 0} {
    putquick "NOTICE $otbn1 :\0033You currently have a password set - type \0034\002/msg $botnick auth <password>\002\0033 to login."
  } else {
    putquick "NOTICE $otbn1 :\0034You \002!MUST!\002 set a password for future identification purposes.You will need this password to log in every time you want to use the bot by typing\00312 \002/msg $botnick auth <password>\002"
    putquick "NOTICE $otbn1 :\00312You may do so by typing\00312 \002/msg $botnick pass <password>\002 now"
    putquick "NOTICE $otbn1 :\00312To learn how to use this bot, type\00312 \002$botnick commands \002 now"
    putquick "NOTICE $otbn1 :\0034WARNING:\00312 Do not authenticate until you receive a notice that your password has been set"
  }



# Done - Exit Procedure
# ---------------------
  return 0
}

